package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaProfitReportItemDetail1Dao;
import com.qymerge.manage.base.ro.MetaProfitReportItemDetail1RO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: MetaProfitReportItemDetail1DaoImpl</p>
 *<p>Description: 报表项目与科目的关系</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaProfitReportItemDetail1DaoImpl extends BaseDao implements IMetaProfitReportItemDetail1Dao {


//    private String dwd_MCRS_MDM_meta_ProfitReportItemDetail1 = "SELECT Id,ReportItemId,AccountViewId FROM dwd_MCRS_MDM_meta_ProfitReportItemDetail1 WHERE 1=1 ";
    private String dwd_MCRS_MDM_meta_ProfitReportItemDetail1 = "SELECT prid1.Id as Id,prid1.ReportItemId as ReportItemId,prid1.AccountViewCode as AccountViewCode,pri.Id as ReportItemID, pri.Name as ReportItemName, pri.ShowName as ReportItemShowName, av.Code as AccountViewCODE, av.Name as AccountViewName " +
        "FROM dwd_MCRS_MDM_meta_ProfitReportItemDetail1 prid1 " +
        "left join dwd_MCRS_MDM_meta_ProfitReportItem pri on prid1.ReportItemId = pri.Id " +
        "left join dwd_MCRS_MDM_meta_AccountView av on prid1.AccountViewCode = av.Code WHERE 1=1 ";

    @Override
    public List list(MetaProfitReportItemDetail1RO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_ProfitReportItemDetail1;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and prid1.ReportItemId=:ReportItemId";
        paramMap.put("ReportItemId", ro.getReportItemId());

        // 分页数据获取
        sql += " order by prid1.Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaProfitReportItemDetail1RO>(MetaProfitReportItemDetail1RO.class));
    }

    @Override
    public int total(MetaProfitReportItemDetail1RO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_ProfitReportItemDetail1 where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and ReportItemId=:ReportItemId";
        paramMap.put("ReportItemId", ro.getReportItemId());

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaProfitReportItemDetail1RO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_ProfitReportItemDetail1);
        sql.append("and prid1.Id=?");
        RowMapper<MetaProfitReportItemDetail1RO> rowMapper = new BeanPropertyRowMapper<MetaProfitReportItemDetail1RO>(MetaProfitReportItemDetail1RO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int insert(MetaProfitReportItemDetail1RO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_meta_ProfitReportItemDetail1(ReportItemId, AccountViewCode) values(?,?)");

        List params = new ArrayList();
        params.add(ro.getReportItemId());
        params.add(ro.getAccountViewCode());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_ProfitReportItemDetail1 where Id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };

        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

    @Override
    public int remove(Long reportItemId) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_ProfitReportItemDetail1 where ReportItemId=?";

        return getJdbcTemplate().update(sql, reportItemId);
    }


}
